// pages/post/post-detail/post-detail.js
import {
  DBPost
} from '../../../db/DBPost.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var postId = options.id;
    this.dbPost = new DBPost(postId);
    this.postData = this.dbPost.getPostItemById().data;
    this.setData({
      post: this.postData
    })
    this.addReadingTimes();
    this.setAniation();
    this.setCollectionAnimation();

  },
  // 收藏事件
  onCollectionTap: function(event) {
    var newData = this.dbPost.collect();
    /**
     * mask 主要用来防止用户连续点击收藏按钮
     * true 时连续点击土收藏图标，图标不会连续作响应
     * false 时连续点击土收藏图标，图标会连续作响应
     */
    // wx.showToast({
    //   title: newData.collectionStatus ? '收藏成功' : '收藏失败',
    //   duration: 1000,
    //   icon: 'success',
    //   mask: true
    // })
    this.setData({
      'post.collectionStatus': newData.collectionStatus,
      'post.collectionNum': newData.collectionNum
    })

    this.animationCollection.scale(2).step();
    this.setData({
      animationCollection: this.animationCollection.export()
    })

    setTimeout(function(){
      this.animationCollection.scale(1).step();
      this.setData({
        animationCollection: this.animationCollection.export()
      })
    }.bind(this),300)

  },
  // 点赞事件
  onUpTap: function(event) {
    var newData = this.dbPost.up();
    // wx.showToast({
    //   title: newData.upStatus ? '点赞成功' : '点赞失败',
    //   duration: 1000,
    //   icon: 'success',
    //   mask: true
    // })
    this.setData({
      'post.upStatus': newData.upStatus,
      'post.upNum': newData.upNum
    })

    //放大
    this.animationUp.scale(2).step();
    this.setData({
      animationUp: this.animationUp.export()
    })
    setTimeout(function(){
      //恢复原装
      this.animationUp.scale(1).step();
      this.setData({
        animationUp: this.animationUp.export()
      })
    }.bind(this),300)


  },

  //评论事件
  onCommentTap: function(event) {
    var postId = event.currentTarget.dataset.postId;
    wx.navigateTo({
      url: '../post-comment/post-comment?id=' + postId,
    })

  },

  // 阅读量+1
  addReadingTimes: function() {
    this.dbPost.addReadingTimes();
  },

  //音乐播放器的开关 
  onMusicTap: function() {
    if (this.data.isPlayingMusic) {
      wx.pauseBackgroundAudio();
      this.setData({
        isPlayingMusic: false
      })
    } else {
      wx.playBackgroundAudio({
        dataUrl: this.postData.music.url,
        coverImgUrl: this.postData.music.coverImg,
        title: this.postData.music.title,
        success:function(data){
          console.log("success");
        },
        fail: function (data) {
          console.log("fail");
        }
        
      })
      this.setData({
        isPlayingMusic: true
      })
    }
  },
  
  // 点赞的动画效果
  setAniation:function() {
    var animationUp = wx.createAnimation({
      timingFunction:'ease-in-out'
    })
    this.animationUp = animationUp
  },

  //收藏的动画效果
  setCollectionAnimation:function(){
    var animationCollection =wx.createAnimation({
      timingFunction:'ease-in-out'
    })
    this.animationCollection = animationCollection
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // 动态设置导航栏标题
    wx.setNavigationBarTitle({
      title: this.postData.title,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   * 只用定义了此事件处理函数，右上角菜单才会显示“分享”按钮
   */
  onShareAppMessage: function() {
      return {
        title:this.postData.title,
        desc:this.postData.content,
        path:"/pages/post/post-detail-post-detail"
      }
  }
})